package cn.jasonone.bbs.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.jasonone.bbs.bean.UserInfo;
import cn.jasonone.bbs.consts.ApplicationConsts;
import cn.jasonone.bbs.service.UserInfoService;

@Controller
@SessionAttributes(ApplicationConsts.LOGIN_STATUS_SESSION)
public class UserInfoController {
	@Resource
	private UserInfoService userInfoService;
	@GetMapping("/login")
    public String login() {
    	return "login";
    }
	@PostMapping("/login/username")
	@ResponseBody
    public String loginUserName(String userName) {
		String msg = userInfoService.findByName(userName);
    	return msg;
    }
	
	@PostMapping("/login")
	public String login(UserInfo user,ModelMap map) {
		String msg="";
		UserInfo userInfo = userInfoService.login(user);
		if(userInfo==null) {
			msg="密码错误";
			map.put("msg", msg);
			return "login";
		}
		map.put(ApplicationConsts.LOGIN_STATUS_SESSION	, userInfo);
    	return "redirect:/";
	}
}
